package com.example.dzl.controller;

import com.example.dzl.VO.DailyActiveUsersVO;
import com.example.dzl.service.UserActivityService;
import com.example.dzl.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {

    @Autowired
    private UserActivityService userActivityService;

    // 获取日活跃用户数
    @GetMapping("/daily-active-users")
    public BaseResult<List<DailyActiveUsersVO>> getDailyActiveUsers() {
        List<DailyActiveUsersVO> dailyActiveUsers = userActivityService.getDailyActiveUsers();
        if (dailyActiveUsers != null && !dailyActiveUsers.isEmpty()) {
            return BaseResult.success(dailyActiveUsers);  // 返回成功结果
        } else {
            return BaseResult.error("Failed to retrieve daily active users");
        }
    }

    @GetMapping("/daily-active-users2")
    public BaseResult<List<DailyActiveUsersVO>> getDailyActiveUsers2(@RequestParam(defaultValue = "daily") String timePeriod) {
        List<DailyActiveUsersVO> dailyActiveUsers = null;

        // 根据请求的时间范围，调用服务层不同的查询方法
        switch (timePeriod) {
            case "monthly": // 获取月活跃用户数
                dailyActiveUsers = userActivityService.getMonthlyActiveUsers();
                break;
            case "quarterly": // 获取季活跃用户数
                dailyActiveUsers = userActivityService.getQuarterlyActiveUsers();
                break;

            case "semiannual": // 获取半年活跃用户数
                dailyActiveUsers = userActivityService.getSemiannualActiveUsers();
                break;
            case "daily": // 获取日活跃用户数
            default:
                dailyActiveUsers = userActivityService.getDailyActiveUsers();
                break;
        }

        if (dailyActiveUsers != null && !dailyActiveUsers.isEmpty()) {
            return BaseResult.success(dailyActiveUsers);  // 返回成功结果
        } else {
            return BaseResult.error("Failed to retrieve active users");
        }
    }
}

